<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="项目名称" prop="channelName">
                <el-input
                  class="w-200"
                  v-model="queryParams.channelName"
                  placeholder="请输入项目名称"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <!-- 新增渠道 -->
    <el-dialog
      :visible.sync="channeldialogVisible"
      width="400px"
      :title="title"
      class="no-dialog-padding"
      @close="channelCancle"
    >
      <span>
        <div class="source_box">
          <el-form ref="channelForm" :rules="channelRules" :model="channelForm">
            <el-row>
              <el-col>
                <el-form-item label="渠道名称" prop="channelName">
                  <el-input
                    class="w-100"
                    v-model="channelForm.channelName"
                    placeholder="请输入渠道名称"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-form-item label="排序" prop="channelSeq">
                <el-input-number
                  class="w-100 text-left"
                  placeholder="请输入"
                  v-model="channelForm.channelSeq"
                  :controls="false"
                ></el-input-number>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="渠道编码" prop="channelCode">
                <el-input
                  class="w-100 text-left"
                  placeholder="请输入"
                  v-model="channelForm.channelCode"
                ></el-input>
              </el-form-item>
            </el-row>
          </el-form>
        </div>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancel_btn" @click="channelCancle">取 消</el-button>
        <el-button
          type="primary"
          @click="channelSubmit"
          :loading="channelLoading"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">营销渠道</div>
        <div class="op_right">
          <button class="primary_button" @click="handleAdd">新增渠道</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="channelsList" stripe style="width: 100%">
          <el-table-column prop="channelSeq" label="排序"></el-table-column>
          <el-table-column
            prop="channelCode"
            label="渠道编码"
          ></el-table-column>
          <el-table-column
            prop="channelName"
            label="渠道名称"
          ></el-table-column>
          <el-table-column prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="edit(scope.row)"
                >编辑</el-button
              >
              <el-button size="mini" type="text" @click="dele(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getChannelsList"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  getChannels,
  CreateChannels,
  UpdateChannels,
  getChannelsDetail,
  DeleteChannels,
} from "@/api/bpm/channels";
import { checkEnglish } from "@/utils/regular";
export default {
  name: "EryuoaStoreFrontendtabList",

  data() {
    return {
      loading: false,
      channelsList: [],
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      total: 0,
      title: "",
      channeldialogVisible: false,
      channelLoading: false,
      CreateClienLoading: false,
      channelForm: {
        channelName: "",
        channelSeq: "",
        channelId: "",
        channelCode: "",
      },
      channelRules: {
        channelName: [
          { required: true, message: "请输入渠道名称", trigger: "blur" },
        ],
        channelSeq: [
          { required: true, message: "请输入排序", trigger: "blur" },
        ],
        channelCode: [
          { required: true, message: "请输入渠道编码" },
          {
            validator: this.CheckEnglish,
            trigger: "blur",
          },
        ],
      },
    };
  },

  created() {
    this.getChannelsList();
  },

  mounted() {},

  methods: {
    CheckEnglish(rule, value, callback) {
      if (!checkEnglish(value) && !!value) {
        callback(new Error("只能输入英文字母"));
      } else {
        callback();
      }
    },

    // 营销渠道列表
    getChannelsList() {
      this.loading = true;
      getChannels(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.channelsList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getChannelsList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
    },

    // 表单重置
    resetFormData(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 列表 删除
    dele(row) {
      console.log("a", row);
      this.$confirm("营销渠道删除后无法撤销, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteChannels({ id: row.channelId }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.getChannelsList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    channelSubmit() {
      console.log(this.channelForm);
      this.$refs["channelForm"].validate((valid) => {
        if (valid) {
          if (this.CreateClienLoading) {
            return false;
          }

          if (!this.channelForm.channelId) {
            this.NewAddSource();
          } else {
            this.UpdateSource();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 详情
    getChannelsDetails(row) {
      getChannelsDetail({ id: row.channelId })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.channelForm = {
              channelName: data.channelName,
              channelSeq: data.channelSeq,
              channelId: data.channelId,
              channelCode: data.channelCode,
            };
          }
        })
        .catch((err) => {});
    },

    // 新建来源 按钮
    NewAddSource() {
      this.CreateClienLoading = true;
      CreateChannels(this.channelForm)
        .then((res) => {
          this.CreateClienLoading = false;
          const { code } = res;
          if (code === 0) {
            this.getChannelsList();
            this.channelCancle();
            this.$modal.msgSuccess("操作成功");
          }
        })
        .catch((err) => {
          this.CreateClienLoading = false;
        });
    },

    // 编辑来源 按钮
    UpdateSource() {
      let channelForm = JSON.parse(JSON.stringify(this.channelForm));
      const { channelId, channelName, channelSeq, channelCode } = channelForm;
      this.CreateClienLoading = true;
      UpdateChannels({
        channelId: channelId,
        channelName: channelName,
        channelSeq: channelSeq,
        channelCode: channelCode,
      })
        .then((res) => {
          this.CreateClienLoading = false;
          const { code } = res;
          if (code === 0) {
            this.getChannelsList();
            this.channelCancle();
            this.$modal.msgSuccess("操作成功");
          }
        })
        .catch((err) => {
          this.CreateClienLoading = false;
        });
    },

    channelCancle() {
      this.channelForm = {
        channelId: "",
        channelName: "",
        channelSeq: "",
        channelCode: "",
      };
      this.CreateClienLoading = false;
      this.$nextTick(() => {
        this.channeldialogVisible = false;
      });
    },

    handleAdd() {
      this.channeldialogVisible = true;
      this.title = "新增渠道";
    },

    edit(row) {
      //   console.log(row);
      this.channeldialogVisible = true;
      this.title = "编辑渠道";
      this.getChannelsDetails(row);
    },
  },
};
</script>

<style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.separate_left {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-right: 12px;
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
}

.check {
  font-size: 12px;
  color: #0052d9;
}

.list_Stores {
  height: 200px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.9);
  line-height: 22px;
  .list_Stores_item {
    height: 46px;
    line-height: 46px;
    background: #ffffff;
    padding-left: 47px;
    border-bottom: 1px solid #e7e7e7;
  }
}

.source_box {
  margin: 0 24px;
}
</style>
